//
//  QJShopCarCell.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/8.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJShopCarCell.h"
@interface QJShopCarCell()
@property (weak, nonatomic) IBOutlet UIView *amountView;

@end

@implementation QJShopCarCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    UIColor *lineColor = [UIColor colorWithHexString:@"dddddd"];
    CGFloat lineWidth = 1;
    // 边框
    self.amountView.layer.borderWidth = lineWidth;
    self.amountView.layer.borderColor = lineColor.CGColor;
    self.amountView.layer.cornerRadius = 5;
    
    // 分割线1
    UIView *line1 = [[UIView alloc] init];
    line1.frame = CGRectMake(20, 0, lineWidth, 20);
    line1.backgroundColor = lineColor;
    [self.amountView addSubview:line1];
    
    // 分割线2
    UIView *line2 = [[UIView alloc] init];
    line2.frame = CGRectMake(40, 0, lineWidth, 20);
    line2.backgroundColor = lineColor;
    [self.amountView addSubview:line2];
    // Initialization code
    self.cellTitle.textColor = YFColor_Text;
    self.cellPrice.textColor = YFColor_Red;
    self.specification.textColor = [UIColor colorWithHexString:@"aaaaaa"];
    self.specification.backgroundColor = YFColor_Background;
    self.cellNumber.textColor = [UIColor colorWithHexString:@"555555"];
     [self.cellSelectBtn setImage:[UIImage imageNamed:@"Sselect"] forState:UIControlStateSelected];
     [self.cellSelectBtn setImage:[UIImage imageNamed:@"Snoselect"] forState:UIControlStateNormal];
}
-(void)cellUpdateWithModel:(ShopCarModel *)model{
    if (model) {
        self.model = model;
       
        [self.cellImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",model.image_url]] placeholderImage:[UIImage imageNamed:@"noImage"]];
        
        
        if ([model.ifSelect isEqualToString:@"1"]) {
            self.cellSelectBtn.selected = YES;
           
        }else{
            self.cellSelectBtn.selected = NO;
           
        }
        self.cellTitle.text = [NSString stringWithoutNilFormart:model.goods_name];
        self.cellPrice.text = [NSString stringWithFormat:@"￥%@",model.price];
        
        // 自适应宽度
        self.specification.text = model.goods_specifications_id;
        CGSize size = [YFGlobalTool sizeWithString:self.specification.text font:self.specification.font];
        // 计算的宽度显示不下?暂时加留白
        self.specifiWidthConstraint.constant = size.width + 15;
        
        
//        NSLog(@"class = %@", [model.goods_specifications_id class]);
        if ([model.goods_specifications_id isEqualToString:@"0"]) {
            self.specification.hidden = YES;
        }else {
            self.specification.hidden = NO;
        }
        NSString *amount = [NSString stringWithoutNilFormart:model.amount];
        self.cellNumber.text = amount;
        self.changeNum = amount;
//        if ([amount isEqualToString:@"1"]) {
//            [self.shopJianBtn setImage:[UIImage imageNamed:@"SDjiangray"] forState:UIControlStateNormal];
//        }else{
//            [self.shopJianBtn setImage:[UIImage imageNamed:@"SDjian"] forState:UIControlStateNormal];
//        }
        if (isLargeriPhone6) {
            self.cellNumber.font = [UIFont systemFontOfSize:14];
        }else {
            self.cellNumber.font = [UIFont systemFontOfSize:13];
        }

    }
}
- (IBAction)shopSelectBtnClickAction:(UIButton *)sender {
     NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    sender.selected = !sender.selected;
    if (sender.selected) {
         [dict setObject:@"1" forKey:@"selected"];
    }else{
         [dict setObject:@"0" forKey:@"selected"];
    }
   
    [dict setObject:self.model.id forKey:@"id"];
   
   [[NSNotificationCenter defaultCenter]postNotificationName:@"GoodsSelectOrDesSelectedNotification" object:dict];
}

// 不好的地方: 全选会调用这里, 又回调到控制器判断一次状态
//- (void)setIsSelect:(BOOL)isSelect {
//    _isSelect = isSelect;
//    if (isSelect) { // 选中
//        self.model.ifSelect = @"1";
//        [self.cellSelectBtn setImage:[UIImage imageNamed:@"Sselect"] forState:UIControlStateNormal];
//    }else { // 取消选中
//        self.model.ifSelect = @"0";
//        [self.cellSelectBtn setImage:[UIImage imageNamed:@"Snoselect"] forState:UIControlStateNormal];
//    }
//    // 其余按钮选中/取消状态判断
//    if (self.shopSelectOrNotBlock) {
//        self.shopSelectOrNotBlock(self.model);
//    }
//}


- (IBAction)shopNumberJianAction:(UIButton *)sender {  //  商品数目减1
    NSInteger num = [self.changeNum integerValue];
    if (num == 1) {
        [MBPHUD showAlertWithTitle:@"商品数量不能为0!"];
    }else{
        num = num - 1;
        self.changeNum = [NSString stringWithFormat:@"%ld",(long)num];
        [self changeShopNumberAction];
    }
}
- (IBAction)shopNumberAddAction:(UIButton *)sender {  //  //  商品数目加1
    NSInteger num = [self.changeNum integerValue];
    num = num + 1;
    self.changeNum = [NSString stringWithFormat:@"%ld",(long)num];
    [self changeShopNumberAction];
}
#pragma mark -- 修改商品数量
- (void)changeShopNumberAction{
    self.model.amount = self.changeNum;
    
    WEAKSELF
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:self.model.id forKey:@"id"];
    [dict setObject:self.changeNum forKey:@"goods_num"];
    
    [WWNetRequest fetchModifyGoodsAmountWithParameters:dict success:^(id response) {
        NSLog(@"修改商品数量 response = %@", response);
        if (weakSelf.shopNumberChangeBlock) {
            weakSelf.shopNumberChangeBlock(weakSelf.model);
            weakSelf.cellNumber.text = weakSelf.changeNum;
        }
    } failure:^(NSDictionary *error) {
        NSLog(@"修改商品数量 error = %@", error);

    }];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
